<?php


function fc_get_mplace_input(
  &$mplace)
{
  $mplace = array();
  $mplace['cname'] = '';
  $mplace['ename'] = '';
  $mplace['street'] = '';
  $mplace['suite'] = '';
  $mplace['city'] = '';
  $mplace['state'] = '';
  $mplace['zip'] = '';
  $mplace['county'] = '';
  $mplace['country'] = '';
  $mplace['lat'] = '';
  $mplace['lng'] = '';
  $mplace['elv'] = '';
  $mplace['phone'] = '';
  $mplace['fax'] = '';
  $mplace['web'] = '';

  if (!isset($_POST['cname']))
  {
    die("no chinese name provided");
  }
  else
  {
    $mplace['cname'] = trim($_POST['cname']);
  }

  if (isset($_POST['ename']))
  {
    $mplace['ename'] = trim($_POST['ename']);
  }
  else
  {
    $mplace['ename'] = '';
  }

  if (!isset($_POST['street']))
  {
    die("no street provided");
  }
  else
  {
    $mplace['street'] = trim($_POST['street']);
  }

  if (isset($_POST['suite']))
  {
    $mplace['suite'] = trim($_POST['suite']);
  }
  else
  {
    $mplace['suite'] = '';
  }

  if (isset($_POST['city']))
  {
    $mplace['city'] = trim($_POST['city']);
  }
  else
  {
    $mplace['city'] = '';
  }

  if (isset($_POST['state']))
  {
    $mplace['state'] = trim($_POST['state']);
  }
  else
  {
    $mplace['state'] = '';
  }

  if (isset($_POST['zip']))
  {
    $mplace['zip'] = trim($_POST['zip']);
  }
  else
  {
    $mplace['zip'] = '';
  }

  if (isset($_POST['phone']))
  {
    $mplace['phone'] = trim($_POST['phone']);
  }
  else
  {
    $mplace['phone'] = '';
  }

  return;
} /* fc_get_mplace_input() */


function fc_get_geocoder(
  &$mplace)
{
  /*
   * Initialize curl.
   */
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  $ggeo_url = "http://maps.google.com/maps/geo?output=xml&key=ABQIAAAA39Rf6UeBvt0cfU8Y0sa5_RQEklNUjcUyNWNtwKDqkVo1a4eajxRjkVPqkbFpOm9njAIvLzSSsszS9Q&q=";
  $qstr = $mplace['street'] . ", " . $mplace['city'] . ", " . $mplace['state'] . ", " . $mplace['zip'];
  $req_url = $ggeo_url . urlencode($qstr);

  curl_setopt($ch, CURLOPT_URL, $req_url);
  $resp = curl_exec($ch);

  $grslt = simplexml_load_string($resp);
  curl_close($ch);

  if ($grslt->Response->Status->code != 200)
  {
    echo "Unable to parse Google response for {$mplace['cname']}!\n";
  }

  $pmark = $grslt->Response[0]->Placemark;

  $mplace['country'] = $pmark->AddressDetails->Country->CountryNameCode;
  $mplace['state']   = $pmark->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName;
  $mplace['county']  = $pmark->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->SubAdministrativeAreaName;
  $mplace['city']    = $pmark->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->LocalityName;
  $mplace['street']  = $pmark->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->Thoroughfare->ThoroughfareName;
  $mplace['zip']     = $pmark->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->PostalCode->PostalCodeNumber;
  list($mplace['lng'], $mplace['lat'], $mplace['elv']) = split(",", $pmark->Point->coordinates);

  return;
} /* fc_get_geocoder() */


function fc_insert_one_mplace_into_db(
  $mplace)
{
  $dbo = new mysqli('localhost', 'dbuser3', 'welcome1', 'db3');
  if (!$dbo)
  {
    echo mysqli_connect_errno();
  }

  if (!$dbo->select_db("db3"))
  {
    die("Failed to select database");
  }

  $cname   = addslashes($mplace['cname']);
  $ename   = addslashes($mplace['ename']);
  $street  = addslashes($mplace['street']);
  $suite   = addslashes($mplace['suite']);
  $city    = addslashes($mplace['city']);
  $state   = $mplace['state'];
  $zip     = $mplace['zip'];
  $county  = addslashes($mplace['county']);
  $country = $mplace['country'];
  $lat     = $mplace['lat'];
  $lng     = $mplace['lng'];
  $elv     = $mplace['elv'];
  $phone   = $mplace['phone'];
  $fax     = $mplace['fax'];
  $web     = addslashes($mplace['web']);

  $sql = "INSERT INTO fmap_mplace SET ";
  if (isset($cname) && !empty($cname))
    $sql = $sql . "cname=_utf8 '$cname', ";
  if (isset($ename) && !empty($ename))
    $sql = $sql . "ename=_utf8 '$ename', ";
  if (isset($street) && !empty($street))
    $sql = $sql . "street='$street', ";
  if (isset($suite) && !empty($suite))
    $sql = $sql . "suite='$suite', ";
  if (isset($city) && !empty($city))
    $sql = $sql . "city='$city', ";
  if (isset($state) && !empty($state))
    $sql = $sql . "state='$state', ";
  if (isset($zip) && !empty($zip))
    $sql = $sql . "zip='$zip', ";
  if (isset($county) && !empty($county))
    $sql = $sql . "county='$county', ";
  if (isset($country) && !empty($country))
    $sql = $sql . "country='$country', ";
  if (isset($lat) && !empty($lat))
    $sql = $sql . "lat='$lat', ";
  if (isset($lng) && !empty($lng))
    $sql = $sql . "lng='$lng', ";
  if (isset($elv) && !empty($elv))
    $sql = $sql . "elv='$elv', ";
  if (isset($phone) && !empty($phone))
    $sql = $sql . "phone='$phone', ";
  if (isset($fax) && !empty($fax))
    $sql = $sql . "fax='$fax', ";
  if (isset($web) && !empty($web))
    $sql = $sql . "web='$web', ";
/*
  if (isset($desc) && !empty($desc))
    $sql = $sql . "desc=_utf8 '$desc', ";
*/

  $sql = substr($sql, 0, -2);

  $result = $dbo->query($sql);
  if ($dbo->affected_rows != 1)
  {
    echo "couldn't insert a record into mplace table";
  }
  else
  {
    echo "successfully inserted the record.";
  }

  $dbo->close();
}


fc_get_mplace_input($mplace);
fc_get_geocoder($mplace);
fc_insert_one_mplace_into_db($mplace);

echo "<a href=\"index.html\">Go back to the main page</a>"

?>
